package com.ecity.panels{
	import com.ecity.util.UIFactory;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import flash.display.Bitmap;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.primitives.Plane;
	
	/**
	 * ...
	 * @author Yicheng.Zhang
	 */
	public class SpinPanel extends Plane {
		public var targetPoint:Plane= new Plane();
		private var _total:int;
		public static var NUM_ROTATE:int= 3;
		public static var THICKNESS:int = 10;
		public static var PANEL_WIDTH:int = 400;
		public static var PANEL_HEIGHT:int = 280;
		private var _initMaterial:MaterialObject3D;
		public var isSpinning:Boolean = false;
		public var isShowing:Boolean = false;
		public var isReseting:Boolean = false;
		public var spinAngle:int = 0;
		public var rotatedAngle:int = 0;
		public var currentId:int = 2;
		private var _perAngle:Number;
		private var _clickedIndex:int;
		
		public function SpinPanel(total:int,material:MaterialObject3D=null, width:Number=0, height:Number=0, segmentsW:Number=0, segmentsH:Number=0){
			super(null, 1, 1, segmentsW, segmentsH);
			_initMaterial = material;
			_total = total;
			this.initSpins();
			this.addChild(targetPoint);
			targetPoint.visible = false;
		}
		
		private function initSpins():void {
			_perAngle = Math.PI * 2 / _total;
			var innerMaterial:BitmapMaterial = new BitmapMaterial(UIFactory.getBitmapData("spinFrame"));
			var outerMaterial:BitmapMaterial = new BitmapMaterial(UIFactory.getBitmapData("spinFramebg"));
			innerMaterial.smooth = true;
			innerMaterial.interactive = true;
			outerMaterial.smooth = true;
			outerMaterial.interactive = true;
			for (var i:int = 0; i < _total; i++) {
				var outer:Plane = new Plane(null, PANEL_WIDTH, PANEL_HEIGHT, 1, 1);
				var inner:Plane = new Plane(null, PANEL_WIDTH, PANEL_HEIGHT, 1, 1);
				var o_h:Number = -0.5 * PANEL_WIDTH /Math.tan(_perAngle/2);
				var i_h:Number = o_h - THICKNESS;
					outer.rotationY = (-i*_perAngle) * (180/Math.PI) + 90;
					outer.x = Math.cos(i * _perAngle) * 600;
					outer.z = Math.sin(i * _perAngle) * 600;
					outer.material =outerMaterial;
				addChild(outer, "outer" + i);
					inner.rotationY = (-i*_perAngle) * (180/Math.PI) + 90;
					inner.x = Math.cos(i * _perAngle) * 595;
					inner.z = Math.sin(i * _perAngle) * 595;
					inner.material = innerMaterial;
					inner.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseClick);
				addChild(inner, "inner" + i);
			}
		}
		

		private function onMouseClick(e:InteractiveScene3DEvent):void {
			if (isSpinning||isReseting) return;
			_clickedIndex = (int(e.target.name) / 2) - 3;
			
			var spinCount:int = currentId - _clickedIndex;
			
			if (spinCount == 0) return;
			trace(spinCount,"---- = ", currentId ,"-", _clickedIndex);
			if (spinCount < -5){
				spinCount = spinCount + 8;
			}
			if (spinCount > 5){
				spinCount = spinCount - 8;
			}
			spinAngle =spinCount * -45;
			spin();
		}
		
		public function menuControlSpin():void {
			spinAngle = 3 * -45;
			_clickedIndex = currentId - 3;
			spin();
		}
		
		private function spin():void {
			if (this.isShowing) {
				rePosition();
			}else{
				if (!isSpinning) {
						trace(currentId, "---------++++-----------");
					isSpinning = true;
					TweenLite.to(this, 1, { rotationY:rotatedAngle+spinAngle,ease:Cubic.easeIn,onComplete:onSpinOver});
				}
			}
		}
		
		private function onSpinOver():void {
			isSpinning = false;
			rotatedAngle = this.rotationY;
			currentId = _clickedIndex > 7?_clickedIndex - 8:_clickedIndex;
			currentId = _clickedIndex < 0?_clickedIndex+8:_clickedIndex;
			showTarget();
		}
		
		private function showTarget():void {
			if (isShowing) return;
			spinAngle = this.rotationY;
			var imoveX:Number = Math.cos(currentId *_perAngle) * -255;
			var imoveZ:Number = Math.sin(currentId * _perAngle) * -255;
			var omoveX:Number = Math.cos(currentId *_perAngle) * -250;
			var omoveZ:Number= Math.sin(currentId *_perAngle) * -250;
			var outer:Plane = this.getChildByName("outer" + currentId) as Plane;
			var inner:Plane = this.getChildByName("inner"+currentId) as Plane;
			TweenLite.to(outer, 0.5, { x:omoveX,y:-60, z:omoveZ, ease:Cubic.easeInOut} );
			TweenLite.to(inner, 0.5, { x:imoveX, y: -60, z:imoveZ, ease:Cubic.easeInOut,onComplete:spinAndShowOver } );
		}
		private function spinAndShowOver():void {
			isShowing = true;
			isSpinning = false;
		}
		
		private function rePosition():void {
			isReseting = true;
			var imoveX:Number = Math.cos(currentId *_perAngle) * 595;
			var imoveZ:Number = Math.sin(currentId * _perAngle) * 595;
			var omoveX:Number = Math.cos(currentId *_perAngle) * 600;
			var omoveZ:Number= Math.sin(currentId *_perAngle) * 600;
			var outer:Plane = this.getChildByName("outer" + currentId) as Plane;
			var inner:Plane = this.getChildByName("inner"+currentId) as Plane;
			TweenLite.to(outer, 0.5, { x:omoveX,y:0, z:omoveZ, ease:Cubic.easeInOut, onComplete:rePositionOver } );
			TweenLite.to(inner, 0.5, { x:imoveX, y:0, z:imoveZ, ease:Cubic.easeInOut, onComplete:spin } );
		}
		
		private function rePositionOver():void {
			isShowing = false;
			isReseting = false;
		}

		
	}

}